Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports ModuleMasters.DNN.Modules.NCAAPoolStandings.Business

Namespace ModuleMasters.DNN.Modules.NCAAPoolStandings

    Public MustInherit Class NCAAPoolStandings
		Inherits Entities.Modules.PortalModuleBase
		Implements Entities.Modules.IActionable

#Region "Controls"
#End Region

#Region "Event Handlers"
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Try

                ' Have not Set Options
                If IsNothing(Settings("PoolBracketModuleId")) Then
                    dgPoolStandings.Visible = False
                    lblSetupModule.Visible = True
                    lblSetupModule.ForeColor = System.Drawing.Color.Red
                    Return
                End If

                lblSetupModule.Visible = False

                'Sample code to get data
                Dim objNCAAPoolStandings As New NCAAPoolStandingsController
                Dim arrPoolStandings As ArrayList
                Dim PoolBracketModuleId As Integer

                If Not Page.IsPostBack Then

                    PoolBracketModuleId = CInt(Settings("PoolBracketModuleId"))
                    arrPoolStandings = objNCAAPoolStandings.ListPoolStandings(PortalId, PoolBracketModuleId)

                    Dim dtPoolStandings As New DataTable("NCAAPoolStandings")
                    Dim dvPoolStandings As DataView

                    dtPoolStandings = PopulateStandingsTable(arrPoolStandings)
                    dvPoolStandings = New DataView(dtPoolStandings)

                    'dgPoolStandings.DataSource = arrPoolStandings
                    dgPoolStandings.DataSource = dvPoolStandings
                    dgPoolStandings.DataBind()

                    ' Manually register the event-handling method for the PageIndexChanged  
                    ' event of the DataGrid control.
                    AddHandler dgPoolStandings.SortCommand, AddressOf dgPoolStandings_SortCommand

                    'Determine which field to display for user name
                    Dim txtUserNameDisplayOption As String
                    If Not Settings("UserNameDisplayOption") Is Nothing Then
                        txtUserNameDisplayOption = CType(Settings("UserNameDisplayOption"), String)
                    Else
                        txtUserNameDisplayOption = "UserLoginName"
                    End If

                    If txtUserNameDisplayOption = "UserLoginName" Then
                        dgPoolStandings.Columns(0).Visible = False
                    Else
                        dgPoolStandings.Columns(1).Visible = False
                    End If

                    'Turn off Max Points Possible
                    dgPoolStandings.Columns(4).Visible = False

                End If
            Catch exc As Exception
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub

        Public Function BuildLink(ByVal ViewUserId As String, ByVal ViewUserName As String) As String
            'builds a link with to the bracket tab passing the userID back as a query parm

            'Dim settingsPoolBracket As DotNetNuke.Entities.Modules.PortalModuleBase
            Dim modController As New DotNetNuke.Entities.Modules.ModuleController
            Dim hash As Hashtable
            hash = modController.GetModuleSettings(CInt(Settings("PoolBracketModuleId")))
            Dim dtPickCutoff As Date

            Dim PickCutoff As Date
            If Not hash("PickCutoff") Is Nothing Then
                PickCutoff = CType(hash("PickCutoff"), Date)
            Else
                PickCutoff = CType("3/19/2008 23:59", Date)
            End If

            If Now() > PickCutoff Then
                Dim bracketTabId As Integer
                If Not IsNothing(Settings("PoolBracketTabId")) Then
                    bracketTabId = CInt(Settings("PoolBracketTabId"))
                End If

                Dim parms(2) As String
                parms(0) = "ViewUserId=" & ViewUserId
                parms(1) = "ViewUserName=" & ViewUserName

                Return "<a href='" & NavigateURL(bracketTabId, "", parms) & "'>" & ViewUserName & "</a>"
            Else
                'we return just the text label
                Return ViewUserName
            End If


        End Function

        Private Function PopulateStandingsTable(ByRef arrPoolStandings As ArrayList) As DataTable

            Dim dt As DataTable = New DataTable
            Dim dc As DataColumn
            Dim dr As DataRow

            ' Create Columns for Standings Table.    
            dc = New DataColumn
            dc.DataType = System.Type.GetType("System.Int32")
            dc.ColumnName = "UserId"
            dt.Columns.Add(dc)

            dc = New DataColumn
            dc.DataType = System.Type.GetType("System.String")
            dc.ColumnName = "UserName"
            dt.Columns.Add(dc)

            dc = New DataColumn
            dc.DataType = System.Type.GetType("System.String")
            dc.ColumnName = "UserFirstLastName"
            dt.Columns.Add(dc)

            dc = New DataColumn
            dc.DataType = System.Type.GetType("System.Int32")
            dc.ColumnName = "TotalPicks"
            dt.Columns.Add(dc)

            dc = New DataColumn
            dc.DataType = System.Type.GetType("System.Int32")
            dc.ColumnName = "CorrectPicks"
            dt.Columns.Add(dc)

            dc = New DataColumn
            dc.DataType = System.Type.GetType("System.Int32")
            dc.ColumnName = "IncorrectPicks"
            dt.Columns.Add(dc)

            dc = New DataColumn
            dc.DataType = System.Type.GetType("System.Int32")
            dc.ColumnName = "TotalPoints"
            dt.Columns.Add(dc)

            dc = New DataColumn
            dc.DataType = System.Type.GetType("System.Int32")
            dc.ColumnName = "PossiblePoints"
            dt.Columns.Add(dc)

            dc = New DataColumn
            dc.DataType = System.Type.GetType("System.Int32")
            dc.ColumnName = "Rank"
            dt.Columns.Add(dc)

            Dim objNCAAPoolStandingsInfo As NCAAPoolStandingsInfo

            Dim i As Integer
            For i = 0 To arrPoolStandings.Count - 1
                objNCAAPoolStandingsInfo = CType(arrPoolStandings(i), NCAAPoolStandingsInfo)
                dr = dt.NewRow
                dr("UserId") = objNCAAPoolStandingsInfo.UserId
                dr("UserName") = objNCAAPoolStandingsInfo.UserName
                dr("UserFirstLastName") = objNCAAPoolStandingsInfo.UserFirstLastName
                dr("TotalPicks") = objNCAAPoolStandingsInfo.TotalPicks
                dr("CorrectPicks") = objNCAAPoolStandingsInfo.CorrectPicks
                dr("IncorrectPicks") = objNCAAPoolStandingsInfo.IncorrectPicks
                dr("TotalPoints") = objNCAAPoolStandingsInfo.TotalPoints
                dr("PossiblePoints") = objNCAAPoolStandingsInfo.PossiblePoints
                dr("Rank") = objNCAAPoolStandingsInfo.Rank

                dt.Rows.Add(dr)
            Next

            ' To persist the data source between posts to the server,
            ' store it in session state.  
            Session("Source") = dt

            Return dt

        End Function


#End Region

#Region "Optional Interfaces"
        Public ReadOnly Property ModuleActions() As Entities.Modules.Actions.ModuleActionCollection Implements Entities.Modules.IActionable.ModuleActions
            Get
                Dim Actions As New Entities.Modules.Actions.ModuleActionCollection
                'Actions.Add(GetNextActionID, Localization.GetString(Entities.Modules.Actions.ModuleActionType.AddContent, LocalResourceFile), Entities.Modules.Actions.ModuleActionType.AddContent, "", "", EditUrl(), False, DotNetNuke.Security.SecurityAccessLevel.Edit, True, False)
                Return Actions
            End Get
        End Property
#End Region

#Region " Web Form Designer Generated Code "

        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub
        Protected WithEvents dgPoolStandings As System.Web.UI.WebControls.DataGrid
        Protected WithEvents lblSetupModule As System.Web.UI.WebControls.Label
        Protected WithEvents lblPlayerName As System.Web.UI.WebControls.Label
        Protected WithEvents lblPlayerUserName As System.Web.UI.WebControls.Label
        Protected WithEvents lnkPlayerName As System.Web.UI.WebControls.HyperLink
        Protected WithEvents lnkPlayerUserName As System.Web.UI.WebControls.HyperLink

        'NOTE: The following placeholder declaration is required by the Web Form Designer.
        'Do not delete or move it.
        Private designerPlaceholderDeclaration As System.Object

        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()
        End Sub

#End Region

        Private Sub dgPoolStandings_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles dgPoolStandings.SortCommand
            ' Retrieve the data source from session state.
            Dim dt As DataTable = CType(Session("Source"), DataTable)

            ' Create a DataView from the DataTable.
            Dim dv As DataView = New DataView(dt)

            Dim SortExprs() As String
            Dim CurrentSearchMode As String, NewSearchMode As String
            Dim ColumnToSort As String, NewSortExpr As String

            '  Parse the sort expression - delimiter space
            SortExprs = Split(e.SortExpression, " ")
            ColumnToSort = SortExprs(0)

            ' If a sort order is specified get it, else default is descending
            If SortExprs.Length() > 1 Then
                CurrentSearchMode = SortExprs(1).ToUpper()
                If CurrentSearchMode = "ASC" Then
                    NewSearchMode = "Desc"
                Else
                    NewSearchMode = "Asc"
                End If
            Else   ' If no mode specified, Default is descending
                NewSearchMode = "Asc"
            End If

            '  Derive the new sort expression. 
            NewSortExpr = ColumnToSort & " " & NewSearchMode

            ' Figure out the column index 
            Dim iIndex As Integer
            Select Case ColumnToSort
                Case "UserName"
                    iIndex = 0
                Case "UserFirstLastName"
                    iIndex = 1
                Case "CorrectPicks"
                    iIndex = 2
                Case "IncorrectPicks"
                    iIndex = 3
                Case "PossiblePoints"
                    iIndex = 4
                Case "TotalPoints"
                    iIndex = 5
                Case "Rank"
                    iIndex = 6
            End Select

            ' alter the column's sort expression 
            dgPoolStandings.Columns(iIndex).SortExpression = NewSortExpr

            ' The DataView provides an easy way to sort. Simply set the
            ' Sort property with the name of the field to sort by.
            dv.Sort = NewSortExpr

            ' Rebind the data source and specify that it should be sorted
            ' by the field specified in the SortExpression property.
            dgPoolStandings.DataSource = dv
            dgPoolStandings.DataBind()
        End Sub
    End Class

End Namespace
